package com.android.base.base.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.base.R;
import com.android.base.view.header.BaseHeaderView;

public abstract class BaseTitleFragment extends BaseFragment {

    private View rootView;

    protected BaseHeaderView mTitleHeaderBar;

    public BaseTitleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView= inflater.inflate(R.layout.fragment_title_base, container, false);
        ((FrameLayout)rootView.findViewById(R.id.layout_container)).addView(inflater.inflate(getLayoutViewId(),null));
        initView();
        onViewLoadFinish(rootView);
        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    protected void onViewLoadFinish(View view){}
    /**
     * 初始化
     *
     * */
    private void initView(){
        mTitleHeaderBar = (BaseHeaderView) rootView.findViewById(R.id.ly_header_bar_title);
    }

    protected abstract int getLayoutViewId();

    /**
     * 设置mTitleHeaderBar背景
     */
    protected void setHeaderViewBackgroundColor(int color) {
        if (mTitleHeaderBar == null) {
            return;
        }
        mTitleHeaderBar.setBackgroundColor(color);
    }

    /**
     * 设置左边文本
     */
    protected void setHeaderLeftTitle(String str) {
        if (mTitleHeaderBar == null || null == str) {
            return;
        }
        mTitleHeaderBar.setHeaderLeftText(str);
    }

    /**
     * 设置标题
     */
    protected void setHeaderTitle(String title) {
        if (mTitleHeaderBar == null || null == title) {
            new Throwable("没有标题");
            return;
        }
        mTitleHeaderBar.setHeaderTitle(title);
    }

    /**
     * 设置右边文本
     */
    protected void setHeaderRightTitle(String str) {
        if (mTitleHeaderBar == null || null == str) {
            return;
        }
        mTitleHeaderBar.setHeaderRightTitle(str);
    }

    public TextView getHeaderRightView() {
        if (mTitleHeaderBar != null) {
            return mTitleHeaderBar.getHeaderRightView();
        }
        return null;
    }

    /**
     * 设置左边图标
     */
    protected void setHeaderLeftDrawable(Drawable drawable) {
        if (mTitleHeaderBar == null) {
            return;
        }
        mTitleHeaderBar.setHeaderDrawableLeft(drawable);
    }

    /**
     * 设置右边图标
     */
    protected void setHeaderRightDrawable(Drawable drawable) {
        if (mTitleHeaderBar == null) {
            return;
        }
        mTitleHeaderBar.setHeaderRightDrawable(drawable);
    }

    /**
     * 设置右边点击监听
     */
    public void setOnClickHeaderRightListener(View.OnClickListener mOnClickListener) {
        mTitleHeaderBar.setHeaderRightClicklistener(mOnClickListener);
    }

    /**
     * 设置左边点击监听
     */
    public void setOnClickHeaderLeftListener(View.OnClickListener mOnClickListener) {
        mTitleHeaderBar.setHeaderLeftClicklistener(mOnClickListener);
    }
}
